<?php
namespace App\Http\Services;
use Mail;
use Mockery\Exception;
use Log;

class MailService
{
    public function contractStartEmail($data, $email, $subject) {
        try {
            Log::debug('sendmail Start:'.now());
            //メールの送信を開始する
            Mail::send("emails.ContractStartEmail", $data, function ($message) use ($email, $subject) {
                $message->from(config('mail.from')['address'], mb_encode_mimeheader(config('mail.from')['name']))
                    ->to($email)
                    ->subject($subject);
            });
            Log::debug('sendmail End:'.now());
        } catch (Exception $e) {
            Log::debug('sendmail error Exception:'.now());
            $this->tokenErrorWithInfo(config('message.S05MAILE_SEND_FAIL'));
        }
    }

    public function ForgetPasswordEmail($data, $email, $subject) {
        try {
            Log::debug('sendmail Start:'.now());
            //メールの送信を開始する
            Mail::send("emails.PasswordForget", $data, function ($message) use ($email, $subject) {
                $message->from(config('mail.from')['address'], mb_encode_mimeheader(config('mail.from')['name']))
                    ->to($email)
                    ->subject($subject);
            });
            Log::debug('sendmail End:'.now());
        } catch (Exception $e) {
            Log::debug('sendmail error Exception:'.now());
        }
    }

    public function pwEmail($data, $email, $subject) {
        try {
            Log::debug('sendmail Start:'.now());
            //メールの送信を開始する
            Mail::send("emails.MemberPasswordRepublish", $data, function ($message) use ($email, $subject) {
                $message->from(config('mail.from')['address'], mb_encode_mimeheader(config('mail.from')['name']))
                    ->to($email)
                    ->subject($subject);
            });
            Log::debug('sendmail End:'.now());
        } catch (Exception $e) {
            Log::debug('sendmail error Exception:'.now());
        }
    }

    public function MemberManageAdd($data, $email, $subject) {
        try {
            Log::debug('sendmail Start:'.now());
            //メールの送信を開始する
            Mail::send("emails.MemberManageAdd", $data, function ($message) use ($email, $subject) {
                $message->from(config('mail.from')['address'], mb_encode_mimeheader(config('mail.from')['name']))
                    ->to($email)
                    ->subject($subject);
            });
            Log::debug('sendmail End:'.now());
        } catch (Exception $e) {
            Log::debug('sendmail error Exception:'.now());
        }
    }

    public function MemberManageEdit($data, $email, $subject) {
        try {
            Log::debug('sendmail Start:'.now());
            //メールの送信を開始する
            Mail::send("emails.MemberManageEdit", $data, function ($message) use ($email, $subject) {
                $message->from(config('mail.from')['address'], mb_encode_mimeheader(config('mail.from')['name']))
                    ->to($email)
                    ->subject($subject);
            });
            Log::debug('sendmail End:'.now());
        } catch (Exception $e) {
            Log::debug('sendmail error Exception:'.now());
        }
    }

    public function MemberManageRestart($data, $email, $subject) {
        try {
            Log::debug('sendmail Start:'.now());
            //メールの送信を開始する
            Mail::send("emails.MemberManageRestart", $data, function ($message) use ($email, $subject) {
                $message->from(config('mail.from')['address'], mb_encode_mimeheader(config('mail.from')['name']))
                    ->to($email)
                    ->subject($subject);
            });
            Log::debug('sendmail End:'.now());
        } catch (Exception $e) {
            Log::debug('sendmail error Exception:'.now());
        }
    }

    public function MemberManageStop($data, $email, $subject) {
        try {
            Log::debug('sendmail Start:'.now());
            //メールの送信を開始する
            Mail::send("emails.MemberManageStop", $data, function ($message) use ($email, $subject) {
                $message->from(config('mail.from')['address'], mb_encode_mimeheader(config('mail.from')['name']))
                    ->to($email)
                    ->subject($subject);
            });
            Log::debug('sendmail End:'.now());
        } catch (Exception $e) {
            Log::debug('sendmail error Exception:'.now());
        }
    }

    public function MemberManageDelete($data, $email, $subject) {
        try {
            Log::debug('sendmail Start:'.now());
            //メールの送信を開始する
            Mail::send("emails.MemberManageDelete", $data, function ($message) use ($email, $subject) {
                $message->from(config('mail.from')['address'], mb_encode_mimeheader(config('mail.from')['name']))
                    ->to($email)
                    ->subject($subject);
            });
            Log::debug('sendmail End:'.now());
        } catch (Exception $e) {
            Log::debug('sendmail error Exception:'.now());
        }
    }

    public function ContractReject($data, $email, $subject) {
        try {
            Log::debug('sendmail Start:'.now());
            //メールの送信を開始する
            Mail::send("emails.ContractReject", $data, function ($message) use ($email, $subject) {
                $message->from(config('mail.from')['address'], mb_encode_mimeheader(config('mail.from')['name']))
                    ->to($email)
                    ->subject($subject);
            });
            Log::debug('sendmail End:'.now());
        } catch (Exception $e) {
            Log::debug('sendmail error Exception:'.now());
        }
    }

    public function ContractAgree($data, $email, $subject) {
        try {
            Log::debug('sendmail Start:'.now());
            //メールの送信を開始する
            Mail::send("emails.ContractAgree", $data, function ($message) use ($email, $subject) {
                $message->from(config('mail.from')['address'], mb_encode_mimeheader(config('mail.from')['name']))
                    ->to($email)
                    ->subject($subject);
            });
            Log::debug('sendmail End:'.now());
        } catch (Exception $e) {
            Log::debug('sendmail error Exception:'.now());
        }
    }

    public function SendMailControllerSendEmail($data, $info, $subject) {
        try {
            Mail::raw($data,  function ($message) use ($info, $subject){
                $message->from(config('mail.from')['address'], mb_encode_mimeheader(config('mail.from')['name']))
                    ->to($info)
                    ->subject($subject);
            });
        } catch(Exception $e) {
            return $e;
        }
    }

    public function SendMailTotControllerSendEmail($data,$email, $company, $subject, $attachment) {
        Mail::send("emails.BatchSendMail", $data, function ($message) use ($email, $company, $subject, $attachment) {
            $message->from(config('mail.from')['address'], mb_encode_mimeheader(config('mail.from')['name']))
                ->to(mb_encode_mimeheader(config('mail.from')['address']), $company)
                ->bcc($email)
                ->subject($subject);
            foreach ($attachment as $filePath) {
                $strA = $filePath['inputOri'];
                $ext = $filePath['inputext'];
                $alias = substr($strA, 0, strpos($strA, "." . $ext));
//                        $message->attach(storage_path($filePath['inputpath']), ['as' => "=?UTF-8?B?" . base64_encode($alias) . "?=." . $ext]);
//                        $message->attach(storage_path($filePath['inputpath']), ['as'=>$alias.".".$ext]);
                $message->attachData($filePath['inputpath'], $alias.".".$ext, [
                    'mime' => 'application/pdf',
                ]);
            }
        });
    }
    public function SendMailTotControllerSendEmailMan($data,$email, $company, $subject, $attachment) {
        Mail::send("emails.BatchSendMailMan", $data, function ($message) use ($email, $company, $subject, $attachment) {
            $message->from(config('mail.from')['address'], mb_encode_mimeheader(config('mail.from')['name']))
                ->to(mb_encode_mimeheader(config('mail.from')['address']), $company)
                ->bcc($email)
                ->subject($subject);
            foreach ($attachment as $filePath) {
                $strA = $filePath['inputOri'];
                $ext = $filePath['inputext'];
                $alias = substr($strA, 0, strpos($strA, "." . $ext));
//                        $message->attach(storage_path($filePath['inputpath']), ['as' => "=?UTF-8?B?" . base64_encode($alias) . "?=." . $ext]);
//                        $message->attach(storage_path($filePath['inputpath']), ['as'=>$alias.".".$ext]);
                $message->attachData($filePath['inputpath'], $alias.".".$ext, [
                    'mime' => 'application/pdf',
                ]);
            }
        });
    }

    public function IwDisposalPowerAttorney($contract,$data, $ssemail, $subject) {
        try {
            Log::debug('sendmail Start:'.now());
            //メールの送信を開始する
            Mail::send("emails.$contract", $data, function ($message) use ($ssemail, $subject) {
                $message->from(config('mail.from')['address'], mb_encode_mimeheader(config('mail.from')['name']))
                    ->to($ssemail)
                    ->subject($subject);
            });
            Log::debug('sendmail End:'.now());
        } catch (Exception $e) {
            Log::debug('sendmail error Exception:'.now());
        }
    }

    public function CloudSignAutoSend($data,$email,$subject,$autoPath,$filesName) {
        try {
            Log::debug('sendmail Start:'.now());
            Mail::send("emails.CertificateBatch",$data, function ($message) use ($email, $subject,$autoPath,$filesName ) {
                $message->from(config('mail.from')['address'], mb_encode_mimeheader(config('mail.from')['name']))
                    ->to($email)
                    ->subject($subject);
                $message->attachData($autoPath, $filesName, ['mime' => 'application/pdf',]);
            });
            Log::debug('sendmail End:'.now());
        return true;
        } catch (Exception $e) {
             Log::debug('sendmail error Exception:'.now());
        return false;
        }
    }

    function MemorandumInformationSendMail($contract,$data,$ssemail,$subject){
        try {
            Log::debug('sendmail Start:'.now());
            Mail::send("emails.$contract", $data, function ($message) use ($ssemail, $subject) {
                $message->from(config('mail.from')['address'], mb_encode_mimeheader(config('mail.from')['name']))
                    ->to($ssemail)
                    ->subject($subject);
            });
            Log::debug('sendmail End:'.now());
        } catch (Exception $e) {
            Log::debug('sendmail error Exception:'.now());
        }
    }
    function CwContractSendMail($contract,$data,$ssemail,$subject){
        try {
            Log::debug('sendmail Start:'.now());
            Mail::send("emails.$contract", $data, function ($message) use ($ssemail, $subject) {
                $message->from(config('mail.from')['address'], mb_encode_mimeheader(config('mail.from')['name']))
                    ->to($ssemail)
                    ->subject($subject);
            });
            Log::debug('sendmail End:'.now());
        } catch (Exception $e) {
            Log::debug('sendmail error Exception:'.now());
        }
    }
    // 一般廃棄物送信
    function GwContractSendMail($contract,$data,$ssemail,$subject){
        try {
            Log::debug('sendmail Start:'.now());
            Mail::send("emails.$contract", $data, function ($message) use ($ssemail, $subject) {
                $message->from(config('mail.from')['address'], mb_encode_mimeheader(config('mail.from')['name']))
                    ->to($ssemail)
                    ->subject($subject);
            });
            Log::debug('sendmail End:'.now());
        } catch (Exception $e) {
            Log::debug('sendmail error Exception:'.now());
        }
    }
    function UserChangeSendMail($data,$email,$subject){
        try {
            Log::debug('sendmail Start:'.now());
            Mail::send("emails.MemberLoginEdit", $data, function ($message) use ($email, $subject) {
                            $message->from(config('mail.from')['address'], mb_encode_mimeheader(config('mail.from')['name']))
                                ->to($email)
                                ->subject($subject);
                        });
            Log::debug('sendmail End:'.now());
        } catch (Exception $e) {
            Log::debug('sendmail error Exception:'.now());
        }
    }
    function UserStopSendMail($data,$email,$subject){
        try {
            Log::debug('sendmail Start:'.now());
            Mail::send("emails.MemberLoginStop",$data,  function ($message) use ($email, $subject){
                $message->from(config('mail.from')['address'], mb_encode_mimeheader(config('mail.from')['name']))
                    ->to($email)
                    ->subject($subject);
            });
            Log::debug('sendmail End:'.now());
        } catch (Exception $e) {
            Log::debug('sendmail error Exception:'.now());
        }
    }
    function UserRestartSendMail($data,$email,$subject){
        try {
            Log::debug('sendmail Start:'.now());
            Mail::send("emails.MemberLoginRestart",$data,  function ($message) use ($email, $subject){
                $message->from(config('mail.from')['address'], mb_encode_mimeheader(config('mail.from')['name']))
                    ->to($email)
                    ->subject($subject);
            });
            Log::debug('sendmail End:'.now());
        } catch (Exception $e) {
            Log::debug('sendmail error Exception:'.now());
        }
    }
    function UserDeleteSendMail($data,$email,$subject){
        try {
            Log::debug('sendmail Start:'.now());
            Mail::send("emails.MemberLoginDelete", $data, function ($message) use ($email, $subject){
                $message->from(config('mail.from')['address'], mb_encode_mimeheader(config('mail.from')['name']))
                    ->to($email)
                    ->subject($subject);
            });
            Log::debug('sendmail End:'.now());
        } catch (Exception $e) {
            Log::debug('sendmail error Exception:'.now());
        }
    }
    function UserCompanyDeleteSendMail($data,$email,$subject,$company){
        try {
            Log::debug('sendmail Start:'.now());
            Mail::send("emails.UserCompanyDelete", $data, function ($message) use ($email, $subject, $company){
                $message->from(config('mail.from')['address'], mb_encode_mimeheader(config('mail.from')['name']))
                    ->to(mb_encode_mimeheader(config('mail.from')['address']), $company)
                    ->bcc($email)
                    ->subject($subject);
            });
            Log::debug('sendmail End:'.now());
        } catch (Exception $e) {
            Log::debug('sendmail error Exception:'.now());
        }
    }
    function CompanyUserEnterSendMail($data,$email,$subject){
        try {
            Log::debug('sendmail Start:'.now());
            Mail::send("emails.MemberLoginNew",$data,  function ($message) use ($email, $subject){
                $message->from(config('mail.from')['address'], mb_encode_mimeheader(config('mail.from')['name']))
                    ->to($email)
                    ->subject($subject);
            });
            Log::debug('sendmail End:'.now());
        } catch (Exception $e) {
            Log::debug('sendmail error Exception:'.now());
        }
    }
    function seMaiSendMail($data,$email,$company,$subject){
        try {
            Log::debug('sendmail Start:'.now());
            Mail::send("emails.ContractReadyEmail", $data, function ($message) use ($email, $company, $subject) {
                $message->from(config('mail.from')['address'], mb_encode_mimeheader(config('mail.from')['name']))
                    ->to($email, $company)
                    ->subject($subject);
            });
            Log::debug('sendmail End:'.now());
        } catch (Exception $e) {
            Log::debug('sendmail error Exception:'.now());
        }
    }
    function contractReadyDetailMail($data, $email, $subject) {
        try {
            Log::debug('sendmail Start:'.now());
            Mail::send("emails.ContractReadyDetailEmail", $data,  function ($message) use ($email, $subject){
                $message->from(config('mail.from')['address'], mb_encode_mimeheader(config('mail.from')['name']))
                    ->to($email)
                    ->subject($subject);
            });
            Log::debug('sendmail End:'.now());
        } catch (Exception $e) {
            Log::debug('sendmail error Exception:'.now());
        }
    }
    function contractDeleteMail($data, $email, $subject)
    {
        try {
            Log::debug('sendmail Start:' . now());
            Mail::send("emails.ContractDeleteEmail", $data, function ($message) use ($email, $subject) {
                $message->from(config('mail.from')['address'], mb_encode_mimeheader(config('mail.from')['name']))
                    ->to($email)
                    ->subject($subject);
            });
            Log::debug('sendmail End:' . now());
        } catch (Exception $e) {
            Log::debug('sendmail error Exception:' . now());
        }
    }
    function provisionsInsertSendMail($data,$email,$subject){
        try {
            Log::debug('sendmail Start:'.now());
            Mail::send(['emails.ContractualProvisionsInsert','emails.ContractualProvisionsInsertText'], $data, function ($message) use ($email, $subject) {
                $message->from(config('mail.from')['address'], mb_encode_mimeheader(config('mail.from')['name']))
                    ->to($email)
                    ->subject($subject);
            });
            Log::debug('sendmail End:'.now());
        } catch (Exception $e) {
            Log::debug('sendmail error Exception:'.now());
        }
    }
    function provisionsChangeSendMail($data,$email,$subject){
        try {
            Log::debug('sendmail Start:'.now());
            Mail::send(['emails.ContractualProvisionsEdit','emails.ContractualProvisionsEditText'], $data, function ($message) use ($email, $subject) {
                $message->from(config('mail.from')['address'], mb_encode_mimeheader(config('mail.from')['name']))
                    ->to($email)
                    ->subject($subject);
            });
            Log::debug('sendmail End:'.now());
        } catch (Exception $e) {
            Log::debug('sendmail error Exception:'.now());
        }
    }
    function provisionsDeleteSendMail($data,$email,$subject){
        try {
            Log::debug('sendmail Start:'.now());
            Mail::send(['emails.ContractualProvisionsDelete','emails.ContractualProvisionsDeleteText'], $data, function ($message) use ($email, $subject) {
                $message->from(config('mail.from')['address'], mb_encode_mimeheader(config('mail.from')['name']))
                    ->to($email)
                    ->subject($subject);
            });
            Log::debug('sendmail End:'.now());
        } catch (Exception $e) {
            Log::debug('sendmail error Exception:'.now());
        }
    }
}
